home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / libs / unixlib.lha / unix / test / execf_test2.c < prev    next >
C/C++ Source or Header  |  1996-07-07  |  2KB  |  115 lines

  1. /* Example program to test execf() */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <sys/wait.h>
  8. #include <errno.h>
  9.  
  10. typedef unsigned char bool;
  11.  
  12. #define FALSE 0
  13. #define TRUE  1
  14.  
  15. static char *program = "child_fun";
  16.  
  17. static char msgbuf[512];
  18.  
  19. void
  20. LogFatal(char *x0, char *x1)
  21. {
  22.     extern char *sys_errlist[];
  23.     static bool entered = FALSE;
  24.  
  25.     if (entered)
  26.         return;
  27.     entered = TRUE;
  28.  
  29.     fprintf(stderr, "%s: ", program);
  30.     if (errno)
  31.         fprintf(stderr, "%s: ", sys_errlist[ errno ]);
  32.     fprintf(stderr, x0,x1);
  33.     fprintf(stderr, "  Stop.\n");
  34.     exit(20);
  35. }
  36.  
  37. void
  38. LogFatalI(char *s, int i)
  39. {
  40.     /*NOSTRICT*/
  41.     LogFatal(s, (char *)i);
  42. }
  43.  
  44.  
  45. int process1(void *data)
  46. {
  47.    char buff[256];
  48.    int ret;
  49.    
  50.    printf("New Process 1\n");
  51.    printf("I got \"%s\"\n", data);
  52.    printf("Hit Return now, please.\n");
  53.    fflush(stdout);
  54.  
  55.    if (!fgets(buff, sizeof(buff), stdin)) {
  56.       strcpy(msgbuf, "fgets() failed!");
  57.       return -1;
  58.    }
  59.  
  60.    if (strcmp(buff, "\n") == 0) {
  61.       strcpy(msgbuf, "I'm returning 0 to you.");
  62.       ret = 0;
  63.    } else {
  64.       buff[strlen(buff)-1] = 0;
  65.       sprintf(msgbuf, "You doesn't simply hit return, but wrote: \"%s\"\n", buff);
  66.       strcat(msgbuf, "so I'm returning the number of characters you wrote.");
  67.       ret = strlen(buff);
  68.    }
  69.  
  70.    return ret;
  71. }
  72.  
  73.  
  74. main(int argc, char **argv)
  75. {
  76. #if 0
  77.    FILE *in, *out;
  78. #endif
  79.    int pid;
  80.    int status;
  81.    int rc;
  82.    char *msg;
  83.    
  84.    if (argc >= 2)
  85.        msg = argv[1];
  86.    else
  87.        msg = "Hello out there!";
  88. #if 0
  89.    /* assume fopen() does't fail ... */
  90.    in = fopen("console:", "r");
  91.    out = fopen("console:", "w");
  92. #endif
  93.    /* Start process 1 */
  94.    printf("Starting process 1\n");
  95. #if 0
  96.    pid = execf(process1, msg, fileno(in), fileno(out), NULL, -1);
  97. #else
  98.    pid = execf(process1, msg, -1, -1, NULL, -1);
  99. #endif
  100.    if (pid < 0)
  101.        LogFatal("Cannot exec %s.", program);
  102.    /* Simply wait child completion */
  103.    printf("Waiting for process1 to finish\n");
  104.    rc =  wait(&status);
  105.    printf("Return from child is %d\n", WEXITSTATUS(status));
  106.    printf("Message from child is:\n%s\n", msgbuf);
  107.    if (rc > 0) {
  108.       if (WIFSIGNALED(status))
  109.           LogFatalI("Signal %d.", WTERMSIG(status));
  110.       if (WIFEXITED(status) && WEXITSTATUS(status))
  111.           LogFatalI("Exit code %d.", WEXITSTATUS(status));
  112.    }
  113.    return(0);
  114. }
  115.